Ext.onReady(function(){

	var dsPQ=new Ext.data.JsonStore({ 
   data:[], 
   fields:["idx","relation","leftParenthesis","fieldname","operator","value","rightParenthesis"]  
});
	var fieldsDef = new Ext.data.JsonStore({ 
    fields: ['value','text','type','data'], 
    data:[ 
        {value:'A.Billno',text:'字符串',type:'string'}, 
        {value:'A.Date',text:'日期',type:'date'}, 
        {value:'A.boolean',text:'布尔',type:'boolean'}, 
        {value:'A.int',text:'整形',type:'int'}, 
        {value:'A.float',text:'浮点',type:'float'}, 
        {value:'A.lookup',text:'Lookup',type:'lookup',data:[['AA','list1'],['BB','list2'],['CC','list3']]}, 
        {value:'A.datalist',text:'自定义列表',type:'lookup',data:[[10,'xxxx1'],[20,'xxxx2'],[30,'xxxx3'],[11,'xxxx11'],[21,'xxxx21'],[31,'xxxx31'],[12,'xxxx22'],[22,'xxxx22'],[32,'xxxx32'],[13,'xxxx13'],[23,'xxxx23'],[33,'xxxx33'],[14,'xxxx14'],[24,'xxxx24'],[34,'xxxx3'],[15,'xxxx15'],[25,'xxxx25'],[35,'xxxx35'],[16,'xxxx16'],[26,'xxxx26'],[36,'xxxx36']]} 
    ] 
}); 

function displayOperator(v){ 
    switch(v){ 
        case 'is null': return '空白'; 
        case 'is not null': return '非空白'; 
        case "like '|%'": return '以...开头'; 
        case "like '%|'": return '以...结尾'; 
        case "like '%|%'": return '包含字符'; 
        case "not like '%|%'": return '不包含字符'; 
        default: return v; 
    } 
}

function displayValue(v, params, record){ 
    var dataType = findRecordValue(fieldsDef,'value',record.get('fieldname'),'type'); 
    var operator = record.get('operator'); 
    if (operator=='is null'||operator=='is not null') return ''; 
    switch(dataType){ 
        case 'date': return v ? v.dateFormat('Y-m-d'): ''; 
        case 'boolean': return (v?'是':'否'); 
        case 'lookup': var data = findRecordValue(fieldsDef,'value',record.get('fieldname'),'data'); 
            for (var i=0; i<data.length; i++) 
                if (v==data[i][0]) return data[i][1]; 
            //return data[v] + ','+data[v,0]+','+data[0,0]; 
            //return data[v,0]+','+data[0,0]; 
        /*case 'string': 
        case 'int': 
        case 'float':*/ 
        default: return v; 
    } 
} 


function fileListChange(field,newValue,oldValue){ 
    //alert(field); 
} 

function findRecordValue(store,prop, propValue,field){ 
    var record; 
    if(store.getCount() > 0){ 
        store.each(function(r){ 
            if(r.data[prop] == propValue){ 
                rrecord = r; 
                return false; 
            } 
        }); 
    } 
    return record ? record.data[field] : ''; 
} 
	var colM=new Ext.grid.ColumnModel([ 
    { 
        header:"关系", 
        dataIndex:"relation", 
        width:50, 
        renderer: function(v){return (v=='and'?'并且':'或者')}, 
        editor:new Ext.form.ComboBox({  
            store: [['and','并且'],['or','或者']], 
            mode: 'local',  
            readOnly: true,  
            triggerAction: 'all',  
            allowBlank: false, 
            //valueField: 'value',  
            //displayField: 'text',  
            editable: false,  
            forceSelection: true,  
            blankText:'请选择' 
        }) 
    },{ 
        header:"括号", 
        dataIndex:"leftParenthesis", 
        width:40, 
        editor:new Ext.form.ComboBox({ 
            store: ['(','((','((','(((','(((('], 
            mode: 'local', 
            triggerAction: 'all', 
            valueField: 'value', 
            displayField: 'text', 
            editable: false 
        }) 
    },{ 
        header:"字段名", 
        dataIndex:"fieldname", 
        width:130, 
        //renderer: function(v, params, record){return record.data['fieldname']}, 
        renderer: function(v){return findRecordValue(fieldsDef,'value',v,'text');}, 
        editor:new Ext.form.ComboBox({ 
            store: fieldsDef, 
            mode: 'local', 
            triggerAction: 'all', 
            valueField: 'value', 
            displayField: 'text', 
            editable: false, 
            listeners:{change:fileListChange} 
        }) 
    },{ 
        header:"运算符", 
        width:80, 
        dataIndex:"operator", 
        renderer: displayOperator 
    },{ 
        header:"内容", 
        dataIndex:"value", 
        width:130, 
        renderer: displayValue 
    },{ 
        header:"括号", 
        width:40, 
        dataIndex:"rightParenthesis", 
        editor:new Ext.form.ComboBox({ 
            store: [')','))',')))','))))'], 
            mode: 'local', 
            triggerAction: 'all', 
            valueField: 'value', 
            displayField: 'text', 
            editable: false 
        }) 
    } 
]); 
	
	var grdDPQuery = new Ext.grid.EditorGridPanel({ 
    height:500, 
    width:490, 
    renderTo:"hello",    
    cm:colM, 
    sm:new Ext.grid.RowSelectionModel({singleSelect:false}), 
    store:dsPQ, 
    region:'center', 
    border: false, 
    enableColumnMove: false, 
    enableHdMenu: false, 
    loadMask: {msg:'加载数据...'}, 
    clicksToEdit:1
	});
});